package com.dengyuanke.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.dengyuanke.entity.ShoppingCart;
import com.dengyuanke.dao.ShoppingCartMapper;
import com.dengyuanke.service.IShoppingCartService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Override
    public Boolean addShoppingCart(ShoppingCart shoppingCart) {
        Boolean flag=false;
        String goodsId = shoppingCart.getGoodsId();
        ShoppingCart sc = this.selectOne(new EntityWrapper<ShoppingCart>().eq("goods_id", goodsId));
        if(sc!=null){
            Integer total = sc.getTotal();
            sc.setTotal(total++);
            sc.setUpdateTime(new Date());
            flag=this.updateById(sc);
        }else {
            shoppingCart.setId(IdWorker.get32UUID());
            shoppingCart.setCreateTime(new Date());
            flag=this.insert(shoppingCart);
        }
        return flag;
    }


}
